//初始化列表数组
var List = [];
var startDate = '';
var endDate = '';
var editUserId = 0;

//分页查询
var limit = 5;// 每页多少条
var currPage = 1;// 当前页
var allPage = 1;// 总页数
var count = 0;// 默认共多少条数据

// var start = 1;// 开始查询条数
// var end = 5;// 结束查询条数
//全局变量
var check = 0;

function getstartDate(){
	startDate = document.getElementById('start').value;
    console.log(startDate);
	
}
function getendDate(){
	endDate = document.getElementById('end').value;
    console.log(endDate);
}





function change2(){
    check = $('#check').val();
    console.log(check);
}


getMyLogistics();
function getMyLogistics(){
	console.log(startDate);
	console.log(endDate);
    console.log(check);
   
   

	$.ajax({
		url:ajax_host+'getMyLogistics.act',
		dataType : 'JSON',
		data:{
			start : startDate,
			end : endDate,
			startpage : currPage,
            limitpage : limit,
            check:check
		},
		success:function(resp){
			
			if(resp.success){
				List = resp.list;
				console.log("查询后列表："+List);
				renderTable(resp.list);
				// 总页数=记录数/每页记录
				count = resp.count;
				console.log('总记录数:'+count)
				allPage=count%limit==0?(count/limit):(parseInt(count/limit+1));
				console.log('总页数:'+allPage);
				$("#pager").html(currPage+"/"+allPage);
			}else{
				layer.alert(resp.errmsg);
			}
        }
	})
}

function renderTable(arr){
	var str = `<tr>
		<th>快件单号</th>
		<th>快件名</th>
		<th>收件人</th>
		<th>快件类型</th>
		<th>相关运费</th>
        <th>审核状态</th>
		<th>快件详细目的地</th>
		<th>寄送状态</th>
        <th>操作</th>
		
	</tr>`;
	for(var i = 0;i<arr.length;i++){
		var a = arr[i];
		str += 
		   `<tr>
			<td>${a.eId}</td>
			<td>${a.eName}</td>
			<td>${a.aTtn}</td>
			<td>${a.tName}</td>
            <td>${a.tMoney}</td>
            <td>`;
			if(a.cId == 1) str +='待审核';
            if(a.cId == 2) str +='审核通过';
            if(a.cId == 3) str +='审核不通过';
			str +=`</td>
            <td>${a.dest}</td>
			<td>`;
			if(a.sendState == 0 && a.cId == 2) str += '准备寄送';
			if(a.sendState == 1 && a.cId == 2) str += '正在处理退单';
			if(a.sendState == 2 && a.cId == 2) str += '已退单';
			if(a.sendState == 3 && a.cId == 2) str += '已寄送';
			if(a.sendState == 4 && a.cId == 2) str += '寄送失败已退还余额';
			if(a.cId == 1) str += '正在审核快件中';
			if(a.cId == 3) str += '审核不通过(违禁品)';
			str +=`</td>
			 <td>`;
			if(a.sendState == 0 && a.cId == 2) {
			str +=`<a href = "###" onclick="del(${a.eId})">退单</a>`;
			str +=`</td>`
		}
		if(a.sendState == 1 && a.cId == 2) {
			str +=`<a href = "###" >正在处理退单</a>`;
			str +=`</td>`
		}

			str+=`</tr>`;
		
	}
	html('table',str);
}

function pagenext() {
	if (currPage + 1 > allPage) {
		layer.alert("当前已经是最后一页");
		return;
	}
	
	currPage = currPage + 1;
	getMyLogistics();
};

function pageback() {
	if (currPage - 1 == 0) {
		layer.alert("当前已经是第一页");
		return;
	}
	
	currPage = currPage - 1;
	getMyLogistics();
};

//退单事件
function del(eid){
	console.log('快件退单'); 
	layer.confirm("确定退单吗？", {
		btn: ['确定','取消'] //按钮
	   ,skin: 'layui-layer-lan'
	   ,closeBtn: 0
	   ,anim: 3 //动画类型
   },function(){

	
	//空指针的判断
	var eId = eid;

	$.ajax({
		url:ajax_host+'tuidan.act',
		type:'post',
		dataType:'JSON',
		data:{
			eid : eId
		},
	success:function(resp){
		if(resp.success){
			
			layer.alert('已申请退单要求');
			
			getMyLogistics();
			
		}else{
			layer.alert(resp.errmsg);
		}
		
	}
	})


   });
		
	
}

